<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    #root {
      border: 2px solid pink;
    }

    #box1 {
      background: grey;
      /* overflow: auto; */
    }

    #child {
      margin-bottom: -40px;
      border: 2px solid #000;
    }

    img {
      display: block;
      width: 400px;
      height: 200px;
    }

    #box2 {
      width: 80%;
      height: 100px;
      background: aquamarine;
      border: 2px solid #FFF;
    }
  </style>
</head>

<body>
  <div id="root">
    <div id="box1">
      <div id="child">
        <img src="https://img0.baidu.com/it/u=3399826420,1285863134&fm=26&fmt=auto" alt="">
        <img src="https://img2.baidu.com/it/u=1467000107,2024249069&fm=26&fmt=auto" alt="">
        <img src="https://img2.baidu.com/it/u=3593164165,3970952249&fm=26&fmt=auto" alt="">
        <img src="https://img2.baidu.com/it/u=3141540934,967349315&fm=26&fmt=auto" alt="">
      </div>
    </div>
    <div id="box2">这是box2</div>

    <button id="btn">按钮</button>
  </div>
</body>
<script>
  window.onload = function () {
    const btn = document.getElementById('btn')
    const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver
    //  被监听的对象
    const target  = document.getElementById('child')
    //  创建监听器
    const observer = new MutationObserver(function (mutationsList, observer) {
      console.log('执行了监听函数')
    })
    //  配置需要监听的属性
    const config = { attributes: true, subtree: true, childList: true, characterData: true}
    //  开始监听
    observer.observe(target, config)
    // 结束监听
    // observer.disconnect()
    btn.onclick = function () {
      target.innerHTML += `<img src="https://img2.baidu.com/it/u=3141540934,967349315&fm=26&fmt=auto" alt="123">`
    }
  }
</script>

</html>